#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/module.h>


static struct hrtimer my_hrtimer;

enum hrtimer_restart my_hrtimer_callback(struct hrtimer *timer)
{
    printk(KERN_INFO "hrtimer callback\n");
    hrtimer_forward_now(timer, ktime_set(0, 1000000000));  // 1000毫秒后再次启动定时器
    return HRTIMER_RESTART;
}

static int __init hrtimer_test_init(void)
{
    ktime_t ktime = ktime_set(0, 1000000000);  // 1000毫秒

    printk(KERN_INFO "hrtimer test module loaded\n");

    hrtimer_init(&my_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
    my_hrtimer.function = &my_hrtimer_callback;
    hrtimer_start(&my_hrtimer, ktime, HRTIMER_MODE_REL);
    return 0;
}

static void __exit hrtimer_test_exit(void)
{
    hrtimer_cancel(&my_hrtimer);
    printk(KERN_INFO "hrtimer test module unloaded\n");
}

module_init(hrtimer_test_init);
module_exit(hrtimer_test_exit);
MODULE_LICENSE("GPL");
